# SPDX-License-Identifier: Apache-2.0
BMV2_SWITCH_EXE = simple_switch_grpc
BUILD_DIR = build
P4C = p4c-bm2-ss

# 添加 UserSwitch 支持
PYTHONPATH := $(shell echo $$PYTHONPATH):$(shell pwd)

.PHONY: all run build clean controller

all: run

run: build/load_balance.json
	sudo mn -c
	sudo PYTHONPATH=$(PYTHONPATH) python3 start_mininet.py

build/load_balance.json: load_balance.p4
	mkdir -p $(BUILD_DIR)
	mkdir -p logs
	mkdir -p pcaps
	$(P4C) --p4v 16 --p4runtime-files $(BUILD_DIR)/load_balance.p4info.txt -o $@ $<

build: build/load_balance.json
	@echo "Build completed. Run 'make run' to start the topology"

controller: build/load_balance.json
	@echo "Configuring switch rules..."
	sudo python3 mycontroller.py

clean:
	sudo mn -c
	rm -rf $(BUILD_DIR) pcaps logs
	sudo pkill -f simple_switch_grpc

stop:
	sudo mn -c
	sudo pkill -f simple_switch_grpc
